package com.appcenter.application.mapper;

import com.appcenter.application.dto.externalapi.BusinessSystemOrgDto;
import com.appcenter.application.dto.orgJobs.GetOrgJobsDto;
import com.appcenter.application.dto.orgJobs.OrgJobDto;
import com.appcenter.application.dto.orgs.OrgDto;
import com.appcenter.application.dto.users.UserDto;
import com.appcenter.application.input.orgJob.GetOrgJobsInput;
import com.appcenter.application.model.OrgJobs;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@org.apache.ibatis.annotations.Mapper
@Repository
public interface OrgJobsMapper extends Mapper<OrgJobs> {
    @Select("<script>" +
            " select distinct * from ("+
            " SELECT orgjobs.*,orgs.OrgName,jobs.JobName from orgjobs join orgs on orgjobs.OrgId=orgs.Id join jobs on orgjobs.JobId=jobs.Id" +
            " and jobs.ValidStatus=1 and jobs.Deleted=0 and jobs.VersionNo=#{versionNo} and orgjobs.VersionNo=#{versionNo} and orgs.ValidStatus=1"+
            ""+
            " <when test='appCode != null'>" +
            " AND orgs.AppCode=#{appCode}"+
            " </when>" +
            ""+
            " <when test='appCode==null'>" +
            " AND orgs.AppCode IS NULL"+
            " </when>" +
            ""+
            " <when test='orgId!=null and !penetrate'>" +
            " AND orgs.Id=#{orgId}"+
            " </when>" +
            ""+
            " <when test='orgId!=null and orgIdPath!=null and penetrate'>" +
            " AND orgs.OrgIdPath like concat('%',#{orgIdPath},'%')"+
            " </when>" +
            ""+
            " <when test='keyword!=null'>" +
            " AND jobs.JobName like concat('%',#{keyword},'%')"+
            " </when>" +
            ""+
            " <when test='keyword!=null'>" +
            " union all"+
            " select o.*,s.OrgName,b.JobName from  orgjobs o inner join  jobusers j on o.Id=j.OrgJobId and o.VersionNo=#{versionNo} and j.VersionNo=#{versionNo}" +
            " inner join users u on u.Id=j.UserId  and u.VersionNo=#{versionNo} and (u.AccountIsDisabled=0 or u.AccountIsDisabled is null)   and  u.UserName like concat('%',#{keyword},'%')"+
            " inner  join  orgs s on s.Id=o.OrgId and s.VersionNo=#{versionNo} and s.ValidStatus=1"+
            " inner join jobs b on o.JobId=b.Id and b.ValidStatus=1 and b.Deleted=0 and b.VersionNo=#{versionNo}"+
            ""+
            " <when test='appCode != null'>" +
            " AND s.AppCode=#{appCode}"+
            " </when>" +
            ""+
            " <when test='appCode==null'>" +
            " AND s.AppCode IS NULL"+
            " </when>" +
            ""+
            " <when test='orgId!=null'>" +
            " AND s.Id=#{orgId}"+
            " </when>" +
            ""+
            " </when>" +
            " ) tb"+
            ""+
            " </script>")
    public List<GetOrgJobsDto> GetOrgJobs(GetOrgJobsInput input);

    @Select("select users.* from  users join jobusers on users.Id=jobusers.UserId " +
            "where jobusers.OrgJobId=#{OrgJobId} and (users.AccountIsDisabled=0 or users.AccountIsDisabled is null) and users.VersionNo=#{VersionNo} and jobusers.VersionNo=#{VersionNo}")
    public List<UserDto> GetUsersByOrgJobId(@Param("OrgJobId")String OrgJobId,@Param("VersionNo") Long VersionNo);

    @Select("delete from jobusers where jobId=#{JobId}")
    public void DeleteJobUserById(String JobId);


   // @Select("select orgs.* from  orgs JOIN orgjobs on orgjobs.OrgId=orgs.Id where orgjobs.jobId=#{jobId}")

    @Select("select orgs.* from  orgs JOIN orgjobs on orgjobs.OrgId=orgs.Id JOIN dataversion ON orgs.VersionNo=dataversion.VersionNo" +
            " WHERE orgjobs.VersionNo=dataversion.VersionNo AND orgjobs.VersionNo=dataversion.VersionNo and orgjobs.id=#{orgJobId}")
    OrgDto getOrgByOrgJobId(String orgJobId);



    @Select("select orgs.* from  orgs JOIN orgjobs on orgjobs.OrgId=orgs.Id JOIN dataversion ON orgs.VersionNo=dataversion.VersionNo" +
            " WHERE orgjobs.VersionNo=dataversion.VersionNo AND orgjobs.VersionNo=dataversion.VersionNo and orgjobs.id=#{orgJobId}")
    BusinessSystemOrgDto getBusinessSystemOrg(String orgJobId);
}
